package com.liuwang.meal.manager;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.liuwang.meal.bean.Table;
import com.liuwang.meal.db.DBHelper;
import com.liuwang.meal.db.Tables;

public class TableManager {
	private Context context = null;

	public TableManager(Context context) {
		this.context = context;
	}
	
	// 获取所有桌号
	public List<Table> queryTables(){
		// 获取数据库
		DBHelper db = new DBHelper(this.context);
		SQLiteDatabase database = db.getReadableDatabase();
		String sql = "select _id,num,type,count,remark from "+Tables.TABLE+" order by num";
     	// 执行语句
    	Cursor  cursor = database.rawQuery(sql,null);
    	// 循环遍历结果集
    	List<Table>  list = new ArrayList<Table>();

    	while(cursor.moveToNext()){
    		Integer _id = cursor.getInt(0);
    		Integer num = cursor.getInt(cursor.getColumnIndex("num"));
    		String type = cursor.getString(cursor.getColumnIndex("type"));
    		Integer count = cursor.getInt(cursor.getColumnIndex("count"));
    		String remark = cursor.getString(cursor.getColumnIndex("remark"));
    	    Table table = new Table(_id,num,type,count,remark);
    	    list.add(table);
    	}

    	// 关闭数据库
    	database.close();
    	return list;
	}

	public Table query(String table_no) {
		// 获取数据库
		DBHelper db = new DBHelper(this.context);
		SQLiteDatabase database = db.getReadableDatabase();
		String sql = "select _id,num,type,count,remark from "+Tables.TABLE+" where num=?";
		// 执行语句
    	Cursor  cursor = database.rawQuery(sql, new String[]{table_no});
    	
    	Table table = null;
    	if(cursor.moveToFirst()){
    		Integer _id = cursor.getInt(0);
    		Integer num = cursor.getInt(cursor.getColumnIndex("num"));
    		String type = cursor.getString(cursor.getColumnIndex("type"));
    		Integer count = cursor.getInt(cursor.getColumnIndex("count"));
    		String remark = cursor.getString(cursor.getColumnIndex("remark"));
    	    table = new Table(_id,num,type,count,remark);	
    	}
		return table;
	}
}
